<?php

	class User_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
        
        Validator::is_access($this->session->userdata('user_id'));
    }

    function index(){
        $section = "user";
        $title = _LB_USER;
        $cfer = new Cfer(array(
        $title => base_url('user')));
        
        $filter = array();
        $filter['name'] = $this->input->post('name');
        $filter['id_group'] = $this->input->post('id_group');
        $user = User::getList($filter);

        $this->data['user'] = $user;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->data['group'] = Group::getList();
        $this->load->view('user_list', $this->data);
    }
    
    function change_password($id){

        $section = 'user';
		
        $title = _LB_CHANGE_PASSWORD;
        $cfer = new Cfer(array(
            _LB_USER => 'user',
            $title => base_url('user/change_password/'.$id)));
        $act = $this->input->get_post('act');
    
        $user = new User();

        if (!$id || !$user->get($id)) {
            redirect(base_url('user'));
        }
        
        $new_password = '';
        $confirm_password = '';
        if ($act == ACT_VALID_EDIT) {

      
            $new_password = $this->input->post('new_password');
            $confirm_password = $this->input->post('confirm_password');
            if ($user->validateChangePassword($new_password, $confirm_password)) {
                $user->pass = sha1($new_password);
                $user->update();
                redirect(base_url('user'));
            }
        }

		$array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $this->data['new_password'] = $new_password;
        $this->data['confirm_password'] = $confirm_password;
        $this->data['user'] = $user;
        
        $this->data['title'] = $title;
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('user');
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->load->view('change_password', $this->data);
    }
    
    function detail($id){

        $section = 'user';
		
        $title = _LB_USER_DETAIL;
        $cfer = new Cfer(array(
            _LB_USER => 'user',
            $title => base_url('user/detail/'.$id)));
       
        $user = new User();

        if (!$id || !$user->get($id)) {
            redirect(base_url('user'));
        }
        
        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $this->data['user'] = $user;
        
        $this->data['title'] = $title;
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('user');
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->load->view('user_detail', $this->data);
    }

    function delete($id = null) {
        if($id == $this->session->userdata('user_id')) {
            return redirect(base_url('user'));
        }
        $user = new User();

        if (!$id || !$user->get($id)) {
                redirect(base_url('user'));
        }
        $user->delete();
        redirect(base_url('user'));
    }
    
    function form($id = null) {
        
        
        $section = 'user';
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_USER => 'user',
            _LB_EDIT_USER => base_url('user/form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_USER => 'user',
            _LB_ADD_USER => base_url('user/form/')));
        }
        
        $act = $this->input->get_post('act');
    
        $user = new User();

        if ($id && !$user->get($id)) {
                redirect(base_url('user'));
        }
        
        if ($act == ACT_VALID_EDIT) {
            $user->first_name = $this->input->post('first_name');
            $user->last_name = $this->input->post('last_name');
            
            if(!$id || $id != $this->session->userdata('user_id')) {
                $user->email = $this->input->post('email');
            }
            $user->first_name = $this->input->post('first_name');
            $confirm_password = '';
            if(!$id) {
                $user->pass = $this->input->post('password');
                $confirm_password = $this->input->post('confirm_password');
            }
            $user->id_user_group = $this->input->post('id_group');
            $user->disabled = 0;
            if ($user->validateInput($confirm_password)) {
                if ($id) {
                    $user->update();
                } else {
                    $user->pass = sha1($user->pass);
                    $user->insert();
                }
                if ($this->session->userdata('id_group') == EDITOR) {
                    redirect(base_url('user/detail/'.$id));
                }
                redirect(base_url('user'));
            }
        }

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $title = _LB_USER;
        $this->data['cfer'] = $cfer;
        $this->data['user'] = $user;
        $this->data['title'] = $title;
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('user');
        $this->data['section'] = $section;
        $this->data['group'] = Group::getList(); 
        $this->load->view('user_form', $this->data);
    }
}
